package bill.study.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class IsItCopied {
	public static void main(String [] arts){
		isItCopiedWhenDumpedIntoAnotherCollection();
		isItCopiedWhenDumpedIntoAnArray();
		isItCopiedWhenDumpFromArrayToList();
		isItCopiedUsingSystemArrayCopy();
	}
	
	static void isItCopiedWhenDumpedIntoAnotherCollection(){
		System.out.println("is it copied when dumped into another collection?");
		Set<StringBuffer> set = new HashSet<StringBuffer>();
		StringBuffer sbf1 = new StringBuffer("public");
		StringBuffer sbf2 = new  StringBuffer("private");
		set.add(sbf1);
		set.add(sbf2);
		System.out.println(set);
		List<StringBuffer> list = new ArrayList(set);
		sbf1.reverse();
		sbf2.reverse();
		sbf2 = new StringBuffer("protected");
//		System.out.println(set);
		
		System.out.println(list);
	}
	
	
	static void isItCopiedWhenDumpedIntoAnArray(){
		System.out.println("is it copied when dumped into an array?");
		
		Set<StringBuffer> set = new HashSet<StringBuffer>();
		StringBuffer sbf1 = new StringBuffer("public");
		StringBuffer sbf2 = new  StringBuffer("private");
		set.add(sbf1);
		set.add(sbf2);
		System.out.println(set);
		
		StringBuffer[] sbArray = set.toArray(new StringBuffer[0]);
		
		sbArray[0].reverse();
		sbArray[1].reverse();
	
//		System.out.println(set);
		
		System.out.println(set);
	}
	
	
	static void isItCopiedWhenDumpFromArrayToList(){
		System.out.println("is it copied when dumped from array into a list ?");
		
		
		
		StringBuffer[] sbArray = new StringBuffer[2];
		
		sbArray[0]=new StringBuffer("public");
		sbArray[1]=new  StringBuffer("private");
		System.out.println(sbArray);
		for(int i = 0;  i < sbArray.length; i++){
			System.out.println(sbArray[i]);
		}
		List<StringBuffer> list  =Arrays.asList(sbArray);
		for(Iterator<StringBuffer> it = list.iterator(); it.hasNext();){
			StringBuffer sb=	it.next();
			sb.reverse();
		}
//		System.out.println(set);
		
		System.out.println(sbArray);
		for(int i = 0;  i < sbArray.length; i++){
			System.out.println(sbArray[i]);
		}
	}
	
	static void isItCopiedUsingSystemArrayCopy(){
		System.out.println("is it copied is It Copied Using System.ArrayCopy?");
		
		
		
		StringBuffer[] sbArray = new StringBuffer[2];
		
		sbArray[0]=new StringBuffer("public");
		sbArray[1]=new  StringBuffer("private");
		System.out.println(sbArray);

		for(int i = 0;  i < sbArray.length; i++){
			System.out.println(sbArray[i]);
		}
		StringBuffer[] newOne = new StringBuffer[2];
		System.arraycopy(sbArray, 0, newOne, 0, 2);
		newOne[0].reverse();
		newOne[1].reverse();

//		System.out.println(set);
		
		System.out.println(sbArray);
		for(int i = 0;  i < sbArray.length; i++){
			System.out.println(sbArray[i]);
		}
	}
}
